package com.juku2024.juku.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.juku2024.juku.base.BaseApplication
import com.juku2024.juku.db.dao.*
import com.juku2024.juku.db.entity.*

@Database(
    entities =
    [
        FavoriteEntity::class,
        HistoryEntity::class,
        AppConfigEntity::class,
        SearchHistoryEntity::class,
        VersionEntity::class,
        HomeModel::class,
        AlbumModel::class,
        VideoDetailModel::class,
        VideoVersionEntity::class,
        VideoRecommendModel::class,
        UnlockMoreModel::class,
        HotSearchModel::class,
        HomeGenreModel::class,
        LimitEntity::class,
        VideoUpdateEntity::class,
        NoteModel::class,
        NoticeDetailEntity::class,
        VideoPlayHistoryEntity::class,
        NoteExportEntity::class,
        VideoEntity::class,
    ],
    version = 1,
)
abstract class NoteRoomUtils : RoomDatabase() {

    companion object {

        @Volatile
        private var INSTANCE: NoteRoomUtils? = null

        fun getInstance(): NoteRoomUtils {
            if (INSTANCE == null) {
                INSTANCE = synchronized(this) {
                    INSTANCE ?: buildDatabase(BaseApplication.context).also { INSTANCE = it }
                }
            }
            return INSTANCE!!
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(
            context.applicationContext, NoteRoomUtils::class.java,
            "JuKu.db"
        ).allowMainThreadQueries()
            .fallbackToDestructiveMigrationOnDowngrade()
            .build()
    }

    abstract fun noteDao(): NoteDao
    abstract fun searchKeywordsDao(): SearchKeywordsDao
    abstract fun historyDao(): HistoryDao
    abstract fun favoriteDao(): FavoriteDao
    abstract fun appConfigDao(): AppConfigDao
    abstract fun versionDao(): VersionDao
    abstract fun limitDao(): LimitDao
    abstract fun homeTabDao(): HomeTabDao
    abstract fun albumDao(): AlbumDao
    abstract fun videoVersionDao(): VideoVersionDao
    abstract fun videoDetailDao(): VideoDetailDao
    abstract fun videoRecommendDao(): VideoRecommendDao
    abstract fun hotDao(): HotDao
    abstract fun unlockMoreDao(): UnlockMoreDao
    abstract fun homeGenreDao(): HomeGenreDao
    abstract fun videoUpdateDao(): VideoUpdateDao
    abstract fun noticeDao(): NoticeDao
    abstract fun videoPlayDao(): VideoPlayDao
    abstract fun noteExportDao(): NoteExportDao
    abstract fun videoEntityDao(): VideoEntityDao
}
